/**
  ******************************************************************************
  * @file    APP_RELAY.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   Outputs Driver
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	APP_GLOBALS_RELAY
#define	APP_GLOBALS_RELAY
#include <includes.h>


static void PORLocalRAMReset_RELAY(void);
static void PORInputsStaReset_RELAY(void);
static void RelayOutputsLayerHandle(OS_FLAGS os_value);


/**
  * @brief  LED & Relay(6 output and Crank,Fuel Relay) Output
  * @param  p_arg
  * @return none.
  * @Caller This is a task.
  * @Note   none.
  */
void  App_Task_RELAY (void *p_arg)
{
  uint8_t  os_err;
  OS_FLAGS os_value;
  
  (void)p_arg;
  
  
  /* RAM Reset */
  PORLocalRAMReset_RELAY();
  
  /* HW Interface Initial */
  
  /* Wait for Start action finish */
  OSSemPend(App_Sem_STARTSYNC,0,&os_err);
  
  /* HW Interface Initial */
  RelayOutCfg_Init();

  /* Status initial */
  PORInputsStaReset_RELAY();
  
  OSTimeDlyHMSM(0, 0, 0, 200);
  
  /* Display control */
 	while (DEF_TRUE) 
	{
    /*  Wait for State Update output Flags  */
    os_value = OSFlagPend(App_Flags_RELAY,
                          OSRelayFlags_ALL,
                          OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                          0,
                         &os_err);
    if(os_err!=OS_ERR_NONE)
      SYS_DebugERROR;                                       /* Error- Only for Debug */
    
	  /* Relay Output*/
	  RelayOutputsLayerHandle(os_value);
	}
}


/**
  * @brief  Status Reset for NORM AD
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
static void PORInputsStaReset_RELAY(void)
{

}


/**
  * @brief  Global RAM Reset for RELAY
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
void PORGlobalRAMReset_RELAY(void)
{
	
	
}

/**
  * @brief  Local RAM Reset for RELAY
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
static void PORLocalRAMReset_RELAY(void)
{
	
	
}



static void RelayOutputsWorkLayerHandle(void);

/* Relay Outpus */
static void RelayOutputsLayerHandle(OS_FLAGS os_value)
{
	if(os_value&(OSRelayFlags_UpdRequest|OSRelayFlags_UpdTiming))
	  RelayOutputsWorkLayerHandle();
}




/* Scan Cfg and Outputs */
static void RelayOutputsWorkLayerHandle(void)
{
	uint8_t   i;
	uint16_t RelayOutPcfgBufferCfg[RelayOutPcfgBFTOT_CfgNum];
	uint16_t RelayActiveSta,RelayOutputSta,RelayAuxOrdBit=1;
	
	
	/********************/
	/* Relay Aux Outputs */
  ReadDataBaseDataContent(Adr_RelayOutput1Active,&RelayOutPcfgBufferCfg[RelayOutPcfgBFOrd_Aux1Active],RelayOutPcfgBFTOT_CfgNum);
  for(i=0;i<6;i++)
	{
		if(GetFlagShareBufferDataContent(App_Sem_OUTPUTS,CfgOperaType_OSSem,ShareBuffer_Outputs,RelayOutPcfgBufferCfg[i*2+1]))
		{
			/* OutAux Active sta =Active    */
			RelayActiveSta|=RelayAuxOrdBit;
			
			if(RelayOutPcfgBufferCfg[i*2+0]==RelayOutActiveOrd_InitOpen)
				RelayOutputSta|= RelayAuxOrdBit;
			else
				RelayOutputSta&=~RelayAuxOrdBit;
		}
		else
		{
			/* OutAux Active sta = Inactive */
			RelayActiveSta&=~RelayAuxOrdBit;

			if(RelayOutPcfgBufferCfg[i*2+0]==RelayOutActiveOrd_InitOpen)
				RelayOutputSta&=~RelayAuxOrdBit;
			else
				RelayOutputSta|= RelayAuxOrdBit;
		}
		
		RelayAuxOrdBit<<=1;
	}
	
	/********************/
	/* Relay Fixed Outputs */

	/* Fuel output */
	if(GetFlagShareBufferDataContent(App_Sem_OUTPUTS,CfgOperaType_OSSem,ShareBuffer_Outputs,RelayOutputType_FuelRelay))
	{
	  /* OutAux Active sta =Active    */
	  RelayActiveSta|=RelayAuxOrdBit;
    RelayOutputSta|=RelayAuxOrdBit;
	}
	else
	{
	  /* OutAux Active sta = Inactive */
	  RelayActiveSta&=~RelayAuxOrdBit;
    RelayOutputSta&=~RelayAuxOrdBit;
	}
	RelayAuxOrdBit<<=1;

	/* Crank output */
	if(GetFlagShareBufferDataContent(App_Sem_OUTPUTS,CfgOperaType_OSSem,ShareBuffer_Outputs,RelayOutputType_CrankRelay))
	{
	  /* OutAux Active sta =Active    */
	  RelayActiveSta|=RelayAuxOrdBit;
    RelayOutputSta|=RelayAuxOrdBit;
	}
	else
	{
	  /* OutAux Active sta = Inactive */
	  RelayActiveSta&=~RelayAuxOrdBit;
    RelayOutputSta&=~RelayAuxOrdBit;
	}
	RelayAuxOrdBit<<=1;

	
	/* D+ output */
	if(GetFlagShareBufferDataContent(App_Sem_OUTPUTS,CfgOperaType_OSSem,ShareBuffer_Outputs,RelayOutputType_DplusOutput))
	{
	  /* OutAux Active sta =Active    */
	  RelayActiveSta|=RelayAuxOrdBit;
    RelayOutputSta|=RelayAuxOrdBit;
	}
	else
	{
	  /* OutAux Active sta = Inactive */
	  RelayActiveSta&=~RelayAuxOrdBit;
    RelayOutputSta&=~RelayAuxOrdBit;
	}
	
	
  /******************************************/
	/* Update Relay Outputs(MCU Ports) */
	/******************************************/
	uint16_t TempOutSta=RelayOutputSta;
  for(i=0;i<RelayOutputsTOTNum;i++)
  {
    if(TempOutSta&DEF_BIT_00)
      GPIO_SetBits  (RelayOutCfgTable[i].GPIOx,RelayOutCfgTable[i].GPIO_Pin);
	  else
	  	GPIO_ResetBits(RelayOutCfgTable[i].GPIOx,RelayOutCfgTable[i].GPIO_Pin);
    
    TempOutSta>>=1;
  }

	/******************************************/
	/* Update Status */
	/******************************************/
	TempOutSta=0;
	/* Fuel Relay */
	if(RelayOutputSta&RelayOutBit_FUEL)
		TempOutSta|=(1<<14);
	/* Crank Relay */
	if(RelayOutputSta&RelayOutBit_CRANK)
		TempOutSta|=(1<<12);
	
	ModifyDataBaseDataContent(AdrOutputs_NameOutput1_16,0x0fff,TempOutSta,0);
	
	
	/* Aux1 Relay */
	TempOutSta=0;
	if(RelayOutputSta&RelayOutBit_Aux1)
		TempOutSta|=RelayOutBitOrd_Auxi1;
	if(RelayOutputSta&RelayOutBit_Aux2)
		TempOutSta|=RelayOutBitOrd_Auxi2;
	if(RelayOutputSta&RelayOutBit_Aux3)
		TempOutSta|=RelayOutBitOrd_Auxi3;
  if(RelayOutputSta&RelayOutBit_Aux4)
		TempOutSta|=RelayOutBitOrd_Auxi4;
	if(RelayOutputSta&RelayOutBit_Aux5)
		TempOutSta|=RelayOutBitOrd_Auxi5;
	if(RelayOutputSta&RelayOutBit_Aux6)
		TempOutSta|=RelayOutBitOrd_Auxi6;
	
	ModifyDataBaseDataContent(AdrOutputs_Unnameddigitaloutput1_16,0x03ff,TempOutSta,0);
	
}





/*$PAGE*/#endif	/*	APP_GLOBALS_RELAY	*/
